package com.example.batch.config;

import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/7/19 09:20
 */
@Configuration
public class CommonConfig {

    final JobRepository jobRepository;

    @Autowired
    public CommonConfig(JobRepository jobRepository) {
        this.jobRepository = jobRepository;
    }

    @Bean("taskExecutor")
    public TaskExecutor taskExecutor() {
        return new SimpleAsyncTaskExecutor("spring_batch");
    }

    @Bean
    protected JobLauncher asyncJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        //提供异步处理现场
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor("batch-"));
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }



}
